package start.demo01.datatype.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
import start.demo01.datatype.json.json.JSONPath;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {

    @Test
    public void test() {
        JSONObject json = getJSON();
        JSONPath myPath = new JSONPath(null,"$","store[0][*].remove()");
//        JSONPath myPath = new JSONPath("store[0][*]");
        System.out.println("原数据->\n" + JSON.toJSONString(json,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullListAsEmpty));
        myPath.resolve(json);
        System.out.println("解析结果->\n" + JSON.toJSONString(json,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullListAsEmpty));
    }

    private JSONObject getJSON() {
        String msg;
        String path = "D:\\git\\secops\\secops-data-gateway\\data-editor\\src\\main\\java\\cn\\tf\\mg\\e\\entity\\json\\test.json";
        try (InputStream in = new FileInputStream(path)) {
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            msg = new String(bytes);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return JSON.parseObject(msg);
    }

}
